//
//  MYCityField.m
//  国旗注册界面
//
//  Created by   李小云 on 16/4/18.
//  Copyright © 2016年   李小云. All rights reserved.
//

#import "MYCityField.h"
#import "MYCityModel.h"
#import "SIXCityField.h"

@interface MYCityField () <UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic,strong)NSMutableArray* citysModel;

@property (nonatomic,assign)NSUInteger index;

@property(nonatomic,strong)UIPickerView *pickerView;
@end

@implementation MYCityField

#pragma mark - 数据的懒加载
- (NSMutableArray *) citysModel {
    
    if (_citysModel == nil) {
        _citysModel = [NSMutableArray array];
        NSString *tempStr = [[NSBundle mainBundle]pathForResource:@"provinces.plist" ofType:nil];
        NSArray *tempArr = [NSArray arrayWithContentsOfFile:tempStr];
        
        for (NSDictionary *dict in tempArr) {
            MYCityModel *cityModel = [MYCityModel cityModel:dict];
            [_citysModel addObject:cityModel];
        }
    }
    return _citysModel;
}
#pragma mark - 实现不管用户是通过init还是Xib加载键盘都会获取到自定义的键盘
- (void)awakeFromNib{
    
    [super awakeFromNib];
    [self  getCityField];
    
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
       
        [self  getCityField];
    }
    
    return self;
}

- (void)getCityField {
    self.text = @"湖南 衡阳";
    //从XIB获取自定义的键盘View
    SIXCityField *view = [[[NSBundle mainBundle]loadNibNamed:@"CityField" owner:nil options:nil]lastObject];
    
    for (UIPickerView *pickerView in view.subviews) {
        if ([pickerView isKindOfClass:[UIPickerView class]]) {
            
            _pickerView = pickerView;
        }
    }
    //当点击取消和确认的时候 把键盘的第一响应者取消
    __weak typeof(self) weakSelf = self;
    
    view.myblock = ^(UIButton *btn){
        
        [weakSelf resignFirstResponder];
    };
    //设置键盘的代理 数据源 和输出视图
    _pickerView.delegate = self;
    _pickerView .dataSource = self;
    self.inputView  = view;

}

#pragma mark - UIPickerViewDataSource方法

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    return 2;
    
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    //利用数组模型 赋值
    if (component == 0) {
        return self.citysModel.count;
    } else {
        
      MYCityModel *cityModel = self.citysModel[_index];
      NSArray *arr = cityModel.cities;
      return arr.count;
        
    }
    
}


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == 0) {
        //获取第一行的个数  取决于整个数组有多少个元素
        MYCityModel *cityModel = self.citysModel[row];
        return cityModel.name;
      
    }else {
        //获取模型中的数组有几个元素
        MYCityModel *cityModel = self.citysModel[_index];
        NSArray *arr = cityModel.cities;
        return arr[row];
        
    }
    
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    if (component == 0) {
        //记录当前选中的省的行数
        _index = row;
        //根据第一列的改变  刷新第二列的数据
        [pickerView reloadComponent:1];
    }
    
    //根据当前选中的省
    MYCityModel *cityModel = self.citysModel[_index];
    
    NSString *cityP = cityModel.name;
    //获取到当前的城市数据 返回第几列选中的行数
    NSUInteger cityIndex = [pickerView selectedRowInComponent:1];
    
    NSString *cityName = cityModel.cities[cityIndex];
    
    self.text = [NSString stringWithFormat:@"%@ %@",cityP, cityName];
    

    
    
}
@end
